/**
 * @class   wosh::Request
 * @brief   Requests are the way Methods are called remotely within WOSH system.
 *
 * Conceptually, a request is a triple consisting of an object reference, the name of an
 * operation and a list of actual parameters. An object request is similar to a method invocation
 * in an object-oriented programming language
 *          
 *
 ****************************************************************************
 * @version 0.8.499 $Id: Request.h 2827 2010-08-02 11:01:48Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Core_Request_H__
 #define __WOSH_Core_Request_H__

 #include <woshDefs.h>
 #include <core/MessageContentGeneric.h>

 
namespace wosh {

class Response;

class Request : public MessageContentGeneric
 {
	WOSH_SERIALIZABLE(wosh::Request)
	WOSH_COMPARE_RTTI(wosh::Request)

	public:
		/**
		 * @brief  Empty constructor.
		 */
		Request();
		/**
		 * @brief  Constructor with initializer(s), provided for convenience.
		 * @param [in] method the method name to be executed
		 */
		explicit Request( const std::string& method );
		/**
		 * @brief  Constructor with initializer(s), provided for convenience.
		 * @param [in] method the method name to be executed
		 * @param [in] data the data object (ISerializable abstraction) associated with the request. May be NULL.
		 */
		explicit Request( const std::string& method, ISerializable* data );
		/**
		 * @brief  Copy constructor.
		 * @param m [in] the source object to be copied
		 */
		Request( const Request& m );
		/**
		 * @brief  Empty destructor.
		 */
		virtual ~Request() { }


	public:
		/**
		 * @brief  Create a new Response object from current Request, initialized with arguments.
		 * @param  ReturnValue [in] the returned value of the call
		 * @param  data [in] the data object associated with the request. May be NULL.
		 * @return pointer to a new Response object (ownership is acquired by caller)
		 */
		Response* replyResponse( WRESULT ReturnValue, ISerializable* data ) const;
		/**
		 * @brief  Create a new Response object from current Request, initialized with arguments.
		 * @param  ReturnValue [in] the returned value of the call
		 * @param  message [in] c-string message (NULL terminated) describing the error. NULL value acts as replyResponse(ReturnValue)
		 * @return pointer to a new Response object (ownership is acquired by caller)
		 */
		Response* replyResponse( WRESULT ReturnValue, const char* message ) const;
		/**
		 * @brief  Create a new Response object from current Request, initialized with arguments.
		 * @param  ReturnValue [in] the returned value of the call
		 * @return pointer to a new Response object (ownership is acquired by caller)
		 */
		Response* replyResponse( WRESULT ReturnValue ) const;

/** @name Getters
 * @{
 ******************************************************************************/
	public:
		/**
		 * @brief  Get the method (name) associated with the request
		 * @return constant STL string reference (to internal field)
		 */
		inline const std::string& getMethod() const					{ return this->contentContext; }

		/**
		 * @brief  Get the data object converted as wosh::Variant object.
		 * @return constant reference to request argument (converted).
		 * @warning Casting assumes it is a valid wosh::Variant object, else an empty wosh::Variant object will be returned.
		 */
		const Variant& getArgument() const;

		/**
		 * @brief  Extract first argument from the request-content. Supports Variant and List content.
		 * @param  arg0 [out] the output argument value, updated only on success
		 * @return number of successfully extracteed arguments [0,1]
		 */
		int extractArguments( Variant& arg0 ) const;
		/**
		 * @brief  Extract first and second argument from the request-content. Supports Variant and List content.
		 * @param  arg0 [out] the output first-argument value, updated only on success. using extractArguments(arg0) call, supports Variant content
		 * @param  arg1 [out] the output second-argument value, updated only on success (of both etractions). content must be a List to extract second argument
		 * @return number of successfully extracteed arguments [0,1,2]
		 */
		int extractArguments( Variant& arg0, Variant& arg1 ) const;
		/**
		 * @brief  Extract first, second and third argument from the request-content. Supports Variant and List content.
		 * @param  arg0 [out] the output first-argument value, updated only on success. using extractArguments(arg0) call, supports Variant content
		 * @param  arg1 [out] the output second-argument value, updated only on success (of both etractions). content must be a List to extract second argument
		 * @param  arg2 [out] the output third-argument value, see second-argument strategy
		 * @return number of successfully extracteed arguments [0,1,2,3]
		 */
		int extractArguments( Variant& arg0, Variant& arg1, Variant& arg2 ) const;

		/**
		 * @brief  Get the 'Omit-Response' flag.
		 * @return true when executer shouldn't reply with a wosh::Response message
		 */
		inline bool doOmitResponse() const							{ return this->omitResponseFlag; }

		/**
		 * @brief  Get MessageContent::TYPE of this implementation: a ACTION
		 * @return MessageContent::ACTION
		 */
		virtual MessageContent::TYPE getType() const				{ return MessageContent::ACTION; }

//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:
		/**
		 * @brief  Set the method (name) associated with the request
		 * @param [in] value the method string to be assigned
		 * @see    setContentContext()
		 */
		inline void setMethod( const std::string& value )			{ this->contentContext = value; }

		/**
		 * @brief  Set the 'Omit-Response' flag.
		 * @param [in] value boolean value, when true, executer will omit to reply with a wosh::Response message
		 */
		void setOmitResponse( bool value = true )					{ this->omitResponseFlag = value; }

//@}
/** @name Operators Overloading
 * @{
 ******************************************************************************/
	public:
		virtual Request& operator=(const Request& m);
		virtual bool operator==(const Request &other) const;
		virtual bool operator!=(const Request &other) const	{ return !(*this == other);  }
//@}
	protected:
		virtual void clear();
/** @name Internal vars
 * @{
 ******************************************************************************/
	protected:
		bool omitResponseFlag;		///< tell the executer to omit Response-message

//@}


}; // class def

}; // namespace wosh

#endif //__WOSH_Core_Request_H__
